package cn.mooncode.basketballManage.controller;

import cn.mooncode.basketballManage.pojo.CoachActive;
import cn.mooncode.basketballManage.pojo.CoachSimilarity;
import cn.mooncode.basketballManage.pojo.RespBean;
import cn.mooncode.basketballManage.service.ICoachActiveService;
import cn.mooncode.basketballManage.service.ICoachSimilarityService;
import cn.mooncode.basketballManage.utils.RecommendUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @program: basketballManage
 * @description: 推荐算法类
 * @author: jinweizhi
 * @create: 2021-05-12 08:56
 **/


@RestController
@RequestMapping("/recommend")
public class RecommendController {

    @Autowired
    private ICoachActiveService coachActiveService;
    @Autowired
    private ICoachSimilarityService coachSimilarityService;

    /**
     *获取教练之间相似度
     * @return
     */
    private List<CoachSimilarity> getSimilarityList() {
        List<CoachActive> coachActiveList = coachActiveService.list();
        ConcurrentHashMap<Integer, ConcurrentHashMap<Integer, Long>> activeMap = RecommendUtils.CoachHits(coachActiveList);
        List<CoachSimilarity> similarityList = RecommendUtils.calcSimilarityWithCoach(activeMap);
        for (CoachSimilarity coachSimilarity : similarityList) {
            CoachSimilarity similarity = coachSimilarityService.getOne(new QueryWrapper<CoachSimilarity>().eq("cid", coachSimilarity.getCid()).eq("cid_ref",
                    coachSimilarity.getCid_ref()));
            if (similarity != null) {
                coachSimilarityService.updateById(coachSimilarity);
            } else {
                coachSimilarityService.save(coachSimilarity);
            }

        }
        return similarityList;
    }

    /**
     * 获取推荐场地
     * @param cid
     * @param n
     * @return
     */
    private List<Integer> getSpaceIDList(Integer cid, Integer n) {
        List<CoachSimilarity> coachSimilarityList = getSimilarityList();
        List<CoachActive> coachActiveList = coachActiveService.list();
        List<Integer> similarityList = RecommendUtils.getSimilarityBetweenCoach(cid, coachSimilarityList, n);
        return RecommendUtils.getSpaceID(cid, similarityList, coachActiveList);
    }

    @ApiOperation(value = "获取推荐信息")
    @GetMapping("/")
    public RespBean getRecommendInfos(@RequestParam Integer cid) {
        List<Integer> spaceIdList = getSpaceIDList(cid, 3);
        if (spaceIdList.size() > 0) {
            return RespBean.success("获取推荐信息成功！", spaceIdList);
        }
        return RespBean.error("获取推荐信息失败！");
    }
}
